package stack;

import java.util.Stack;

/**
 * 逆波兰表达式求值 -36
 *
 * @author yyz
 * @create 2024-01-03 18:21
 */
public class Leetcode36 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> s = new Stack<>();
        for (String token : tokens) {
            switch (token) {
                case "+" -> {
                    int n1 = s.pop();
                    int n2 = s.pop();
                    s.push(n2 + n1);
                }
                case "-" -> {
                    int n1 = s.pop();
                    int n2 = s.pop();
                    s.push(n2 - n1);
                }
                case "*" -> {
                    int n1 = s.pop();
                    int n2 = s.pop();
                    s.push(n2 * n1);
                }
                case "/" -> {
                    int n1 = s.pop();
                    int n2 = s.pop();
                    s.push(n2 / n1);
                }
                default -> {
                    s.push(Integer.parseInt(token));
                }
            }
        }
        return s.pop();
    }

    public static void main(String[] args) {
        String[] tokens = {"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
        System.out.println(new Leetcode36().evalRPN(tokens));
    }
}
